package com.xinling.modules.blog.controller;

import com.xinling.common.api.ApiResult;
import com.xinling.modules.blog.model.entity.BlogCategory;
import com.xinling.modules.blog.service.BlogCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "博客分类管理")
@RestController
@RequestMapping("/blog/categories")
public class BlogCategoryController {

    @Autowired
    private BlogCategoryService categoryService;

    @ApiOperation("获取所有分类")
    @GetMapping
    public ApiResult<List<BlogCategory>> getAllCategories() {
        return ApiResult.success(categoryService.findAll());
    }

    @ApiOperation("根据ID获取分类")
    @GetMapping("/{id}")
    public ApiResult<BlogCategory> getCategoryById(@PathVariable Integer id) {
        return ApiResult.success(categoryService.findById(id));
    }

    @ApiOperation("创建分类")
    @PostMapping
    public ApiResult<BlogCategory> createCategory(@RequestBody BlogCategory category) {
        return ApiResult.success(categoryService.create(category));
    }

    @ApiOperation("更新分类")
    @PutMapping("/{id}")
    public ApiResult<BlogCategory> updateCategory(@PathVariable Integer id, @RequestBody BlogCategory category) {
        category.setId(id);
        return ApiResult.success(categoryService.update(category));
    }

    @ApiOperation("删除分类")
    @DeleteMapping("/{id}")
    public ApiResult<Void> deleteCategory(@PathVariable Integer id) {
        categoryService.delete(id);
        return ApiResult.success();
    }
} 